#include "stdafx.h"
#include "ASN1Set.h"

namespace AppliedCryptography
{
	ASN1Set::ASN1Set(ClassBits classBits, ConstructedBit constucted, TypeNumberBits number)
		: ASN1Encodable(classBits, constucted, number)
	{
	};

	ASN1Set::ASN1Set()
		: ASN1Encodable(Universal, Constructed, Set)
	{
	};

	const std::set<ASN1Encodable*>& ASN1Set::getStdContainer() const
	{
		return _value;
	};

	std::set<ASN1Encodable*>& ASN1Set::getStdContainer()
	{
		return _value;
	};

	ASN1Set& ASN1Set::add(ASN1Encodable& obj)
	{
		getStdContainer().insert(&obj);

		return *this;
	};

	unsigned int ASN1Set::size() const
	{
		return getStdContainer().size();
	};

	ASN1Encodable& ASN1Set::get(unsigned int index) const
	{
		std::set<ASN1Encodable*>::const_iterator it = getStdContainer().begin();
		
		for(unsigned int i=0; i<index; ++i) ++it;

		return **it;
	};

	std::vector<byte> ASN1Set::getContentBits() const
	{
		std::set<ASN1Encodable*>::const_iterator it = getStdContainer().begin();
		std::vector<byte> buffer;
		std::vector<byte> unit;

		for(it; it != getStdContainer().end(); ++it)
		{
			unit = (*it)->encode();
			buffer.insert(buffer.end(), unit.begin(), unit.end());
		};

		return buffer;
	};

	ASN1Set ASN1Set::getInstance(ASN1Encodable& obj)
	{
		ASN1Set set;

		if(obj.getTypeBits() == set.getTypeBits())
		{
			return *(ASN1Set*)&obj;
		};

		throw std::exception("Unknow object in getInstance()!");
	};

	ASN1Set& ASN1Set::flush()
	{
		this->getStdContainer().clear();
		return *this;
	};
}; // namespace AppliedCryptography
